#!/bin/bash
# $Id$

# edit descriptors
# validate descriptors
# optionaly add authorized_keys 
# targz it

wd=`pwd`

if [ -d $wd/files ]; then
	. $wd/files/env
	. $wd/files/common-scripts/common.fn
	. $wd/files/common-scripts/common.disks.fn
	misc_files_dir=$wd/files/installcd-initrd/$misc_files_dir
else
	. $(dirname $0)/tools/env
	. $(dirname $0)/tools/common.fn
	. $(dirname $0)/tools/common.disks.fn
fi

edit () {

	local ans s

	for s in TIME MISC NETWORK SERVICES BOOT PACKAGES netinstall/NETWORK AUTOINSTALL; do
		unset ans
		echo -ne "\n Wish to edit the descriptor: $s ? [y/n]: "
		read ans
		if [ "$ans" = "y" -o "$ans" = "Y" ]; then
			vi $nemodir/$s
		else
			continue
		fi
	done
}

edit_disks () {

	local ans d edited cntans

	echo -e "\nEnter the name of the disk related descriptors to edit separated by a space. Or type "none" to skip.\n"
	echo " Example: DISK.hdb DISK.sdc MD.md2 ENCLAYER.root LVM.vgroot MOUNTPOINT.boot .. " 
	echo -n "Descriptor: "

	read ans
	if [ "$ans" = "none" ]; then
		return
	fi

	for d in $ans ; do
		echo " Descriptors edited: $edited "
		sleep 1
		dn="`__strip_bdot $d`" # name
		ds="`__strip_adot $d`" # DESC
		[ "$dn" = "tmp" -o "$dn" = "bck" ] && _err_msg " Cant have 'tmp' or 'bck' as a name "
		if [ ! -f $nemodir/$d ]; then
			cp -v $nemodir/files/`echo $ds|tr '[:upper:]' '[:lower:]'`/${ds}.template \
				$nemodir/${d}
		fi
		echo "Type any key to continue ..."
		read cntans
		vi $nemodir/${d}
		# sfdisk
		if [ "$ds" = "DISK" ]; then
			if [ ! -f $nemodir/sfdisk.${dn} ]; then
				cp -v $nemodir/files/disk/sfdisk.template $nemodir/sfdisk.${dn}
				echo "Type any key to continue ..."
				read cntans
			fi
			vi $nemodir/sfdisk.${dn}
		fi
		edited="$edited $d"
	done
}

# main
if [ $# -lt 2 ]; then
		echo -e "\nERROR: Usage: $0 1:{ edit | edit_disks } 2:{ directory }\n"
		echo -e "	edit        Edits: TIME MISC NETWORK SERVICES BOOT PACKAGES AUTOINSTALL"
		echo -e "	edit_disks  Edits: DISK MD ENCLAYER LVM MOUNTPOINT\n"
		echo -e "\n	 May want to change some vars inside this script ...\n"
		exit 1
fi

cmd=$1
nemodir=$2 ; nemodir="$(basename $2)"
wd="`pwd`"
# err trap
_chk_err ()
{
	local errcode="\nERR:$1:caller=$(caller 0)"
	echo -e "$errcode" |tee -a $logallfile
	exit 1 
}
export -f _chk_err
trap - ERR INT DEBUG RETURN
set -eTE
set -o pipefail
#trap '_chk_err $?'  ERR
trap '_chk_err' 1 2 3 15 ERR
# env tweaks
logallfile=$nemodir/prepare_autoinstall.log
export independent_nemo=1 # these 2 work in conjuction
export prepare_autoinstall=1
export slackware_arch=i486 # i486 or x86_64

echo -e "\n Give me a second ..."

if [ ! -d $nemodir ]; then
	if [ $wd/files/installcd-initrd/mostaza ]; then
		mkdir -p $nemodir
		cp -a $wd/files/installcd-initrd/mostaza/* $nemodir/
		find $nemodir/ -type d -name ".svn" -exec rm -rf {} \; 2>/dev/null || :
		sed -i ${nemodir}/PACKAGES -e "s/##SLKVERSION##/$slackware_version/g"
	else
		echo -e "\nERROR: Cant find the source tree of mostaza."
		echo -e "\nUntar tools/autoinstall/mostaza_template.tar.gz from the CD into $nemodir."
		echo -e "Then run again.\n"
		exit 1
	fi
fi

$cmd

echo -e "\n Will start descriptor validation ..." ; sleep 2
mostaza_desc_dir=$nemodir ; export mostaza_desc_dir
if [ "$cmd" = edit ]; then
	validate_all_descriptors
else
	validate_disk_descriptors
fi

echo -e "\nCopy the public key to $nemodir/keys renaming it to authorized_keys and chmod 600 it."
echo -e "Then run: "tar zcf "<HOSTNAME>.tar.gz" -C $nemodir .""

# vim: ts=2
# prepate_autoinstall.sh: end
